""" this module is in development """


import pickle

class HighScoreTable(object):
    """ A basic high score table. 
    Data returned from getTop, get and to add should be dicts 
    containing whatever info you wish. A backend must be 
    supplied that knows how to transform its data too 
    and from these dicts.
    """

    def __init__(self, backend):
        self.backend = backend

    def getTop(self, number=20):
        return self.backend.get(0, 20)

    def get(self, start, end):
        return self.backend.get(start, end)

    def add(self, info):
        return self.backend.store(info)


class HighScoreBackend(object):
    """ Base class for a high score backend """

    def get(self, start, end):
        raise NotImplemented()

    def add(self, info):
        raise NotImplemented()


class PickleBackend(HighScoreBackend):
    """ Basic high score back end to save to a pickle.
    Date stored must contain a "score" key for this to work.
    """

    def __init__(self, filename):
        try:
            self.scores = pickle.load(open(filename))
        except OSError:
            self.scores = []

        self.sortScores()

    def sortScores(self):
        def sorter(a, b):
            return cmp(a['score'], b['score'])
        self.scores.sort(sorter)

    def get(self, start, end):
        return self.scores[start:end]
        
